﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using UserUtility;
using BLL;


namespace TrainingWorkshop
{
    public partial class UserList : System.Web.UI.Page
    {
        
        protected void Page_Init(object sender, EventArgs e)
        {
            
                Bind();
        }

        private void Bind()
        {
            IUserRepository userRepository = UserRepositoryFactory.InitUserRepository();
            UserListGridView.DataSource = userRepository.GetAllUser();
            UserListGridView.DataBind();

            //DataTable ddropdownlist = BLL.UserManager.GetAllDepartment();
            //DataRow departmentrow = ddropdownlist.NewRow();
            //departmentrow["DepartmentName"] = "(Please Select)";
            //departmentrow["DepartmentId"] = "0";
            //ddropdownlist.Rows.InsertAt(departmentrow, 0);

            DepartmentDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));

            DepartmentDropDownList.DataSource = userRepository.GetAllDepartment();
            DepartmentDropDownList.DataTextField = "DepartmentName";
            DepartmentDropDownList.DataValueField = "DepartmentId";
            DepartmentDropDownList.DataBind();
            
        }

        protected void CreateNewUserButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateNewUser.aspx");
        }

        protected void UserListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int UserId =Convert.ToInt32( (UserListGridView.Rows[e.RowIndex].FindControl("UserIdlabel") as Label).Text);
            IUserRepository userRepositoryDelete = UserRepositoryFactory.InitUserRepository();
            //bool deleteold = delete.Delete(UserId);
            //if ()

                userRepositoryDelete.Delete(UserId);
                Response.Write("<script>alert('Delete successfully');window.location.href='UserList.aspx'</script>");
                Bind();
            //}
            //else
            //{
            //    Response.Write("<script>alert('Failed ')</script>");
            //}
        }

        protected void UserListGridView_RowDataBound(object sender,GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                LinkButton delete=e.Row.FindControl("DeleteLinkButton") as LinkButton;
                if(delete.Text=="Delete")
                delete.Attributes.Add("onclick", "return confirm('Do you want to delete it?')");
            }
        }

        protected void UserListGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //UserListGridView.EditIndex = e.NewEditIndex;
            //Bind();
            //Response.Redirect("CreateNewUser.aspx");
            
            int UserId = Convert.ToInt32((UserListGridView.Rows[e.NewEditIndex].FindControl("UserIdlabel") as Label).Text);
            Response.Redirect("EditUser.aspx?UserId=" + UserId);

        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {

            if (UserNameTextBox.Text == "")
            {
                IUserRepository userRepository = UserRepositoryFactory.InitUserRepository();
                UserListGridView.DataSource = userRepository.GetAllUser();
                UserListGridView.DataBind();

                //UserListGridView.DataSource = BLL.UserManager.GetAllUser();
                //UserListGridView.DataBind();
                //DataTable ddropdownlist = BLL.UserManager.GetAllDepartment();
                //DataRow departmentrow = ddropdownlist.NewRow();
                //departmentrow["DepartmentName"] = "(Please Select)";
                //departmentrow["DepartmentId"] = "0";
                //ddropdownlist.Rows.InsertAt(departmentrow, 0);

                DepartmentDropDownList.Items.Insert(0, new ListItem("", ""));

                DepartmentDropDownList.DataSource = userRepository.GetAllDepartment();
                DepartmentDropDownList.DataTextField = "DepartmentName";
                DepartmentDropDownList.DataValueField = "DepartmentId";
                DepartmentDropDownList.DataBind();
            }
            else
            {
                string username = UserNameTextBox.Text;
                int departmentid = Convert.ToInt32(DepartmentDropDownList.Text);
                IUserRepository userRepository = UserRepositoryFactory.InitUserRepository();
                UserListGridView.DataSource = userRepository.SearchUser(departmentid, username);
                UserListGridView.DataBind();
            }
            
            
        }

        //protected void UserListGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        //{
        //    UserListGridView.EditIndex = -1;
        //    Bind();
        //}

        //protected void UserListGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
        //    User user = new User();
        //    user.UserId = Convert.ToInt32((UserListGridView.Rows[e.RowIndex].FindControl("UserIdlabel") as Label).Text);
        //    user.FirstName = (UserListGridView.Rows[e.RowIndex].FindControl("FirstNameTextBox") as TextBox).Text;
        //    user.LastName = (UserListGridView.Rows[e.RowIndex].FindControl("LastNameTextBox") as TextBox).Text;
        //    //user.DepartmentId = Convert.ToInt32((UserListGridView.Rows[e.RowIndex].FindControl("DepartmentIdTextBox") as Label).Text);
        //    //user.RoleId = Convert.ToInt32((UserListGridView.Rows[e.RowIndex].FindControl("RoleIdTextBox") as Label).Text);
        //    bool delete = BLL.UserManager.Modify(user);
        //    if (delete)
        //    {

        //        Response.Write("<script>alert('Update successfully');window.location.href='UserList.aspx'</script>");
        //        UserListGridView.EditIndex = -1;
        //        Bind();
        //    }
        //    else
        //    {
        //        Response.Write("<script>alert('Failed ')</script>");
        //    }
        //}

        
    }
}